package com.ayi.dao.cartdao;

import com.ayi.bean.Book;
import com.ayi.utils.CartBuilder;
import com.ayi.utils.GetConn;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;


/**
 * @author Ayingzz
 * @create 2021/11/28 14:16
 */
public class QueryCart {


    /**
     * 根据用户名查找订单（购物车）
     * @param username
     * @return List<Book>
     */
    public List<Book> queryCartByUsername(String username) {
        Connection conn = new GetConn().getConnection();
        String sql = "select * from Carts where username = ?";
        try {
            PreparedStatement ps = conn.prepareStatement(sql);
            ps.setString(1, username);
            ResultSet rs = ps.executeQuery();
            List<Book> resList = new CartBuilder().cartBuild(rs);
            return resList;
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 根据用户名查找订单（购物车）
     * @param username
     * @return StringBuilder
     */
    public StringBuilder queryCartWithCreateAStringBuilderByUsername(String username) {
        Connection conn = new GetConn().getConnection();
        String sql = "select * from Carts where username = ?";
        try {
            PreparedStatement ps = conn.prepareStatement(sql);
            ps.setString(1, username);
            ResultSet rs = ps.executeQuery();
            if (rs.next()) {
                return new StringBuilder().append(rs.getString("cartList"));
            }
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return new StringBuilder("");
    }

    /**
     * 根据图书id和用户名，初始化一本书的同时找到它在此用户下的购买数量
     * @return
     */
    public int initBookWithSettingNum(String username, int id) {
        Connection conn = new GetConn().getConnection();
        String sql = "select * from Carts where username = ?";
        try {
            PreparedStatement ps = conn.prepareStatement(sql);
            ps.setString(1, username);
            ResultSet rs = ps.executeQuery();
            if (rs.next()) {
                String str = rs.getString("cartList");
                if (!str.equals("")) {
                    String[] strParsed = str.split("#");
                    for (int i = 0, n = strParsed.length; i < n; i++) {
                        int eqIndex = strParsed[i].indexOf('=');
                        int matchId = Integer.parseInt(strParsed[i].substring(0, eqIndex));
                        if (matchId == id) {
                            int currentLength = strParsed[i].length();
                            int num = Integer.parseInt(strParsed[i].substring(eqIndex + 1, currentLength));
                            return num;
                        }
                    }
                }
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return 0;
    }

}
